/**
 * Class BackgroundImage create and load image
 * Example:
 * form HTML ->
 * 	<div id="image" class="image" data-src="image.png"></div>
 * 	var image = new Image("image");
 * load without data-src ->
 * 	image.setSource("image.png");
 * create without HTML ->
 * 	var image = new Image();
 * 	image.setSource("image.png");
 */
BackgroundImage = (function (_super) {

    __extends(BackgroundImage, _super);
	/**
	 * BackgroundImage constructor
	 * @param [view] {String|HTMLElement} id or html element
	 * @constructor
	 */
    function BackgroundImage(view) {
        BackgroundImage.__super__.constructor.call(this);
		this.setContentView(view);
        this.setSource(this.view.attr("data-src"));
    }
	/**
	 * Overwrite the setContentView and create the HTMLElement if we have not view
	 * @param [view] {String|HTMLElement} id or html element
	 */
	BackgroundImage.prototype.setContentView = function(view){
		var view = view ? view :
			document.createElement("div");
		BackgroundImage.__super__.setContentView.call(this, view);
	};
	/**
	 * Set Image Source set from data-src if we have
	 * @param src {String} Image src
	 */
    BackgroundImage.prototype.setSource = function(src){
        if(!src) return;
        this.source = src;
        this.view.addClass('image loading');
		if(this.source) this.load();
    };
	/**
	 * Load image than set image source to background
	 */
    BackgroundImage.prototype.load = function(){
        var _this = this,
            _image = new Image();

        _image.onload = function(){
            _this.view.style({"background-image": "url("+_this.source+")"});
            _this.view.removeClass("loading");
            if(_this.loadListener) _this.loadListener.call(_this);
        };
        _image.src = this.source;
    };
	/**
	 * Set on image load listener
	 * @param listener {Function}
	 */
    BackgroundImage.prototype.setOnLoadListener = function(listener){
        if(!(listener instanceof Function)) throw "listener is not a Function";
		this.loadListener = listener;
    };

    return BackgroundImage;

})(Base);